package com.swgk.core.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingAdapter;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.adapters.ListenerUtil;

import com.swgk.core.R;
import com.swgk.core.util.ResolutionUtil;


public class TextAreaEditView extends RelativeLayout {

    private Context context;
    private EditText mEditText;
    private TextView mTvName;
    private TextView mTvLine;
    private int MAX_LENGTH;
    private CharSequence teamTemp;
    private OnFocusChangedListener onFocusChangedListener;

    private boolean isShowBottomNumber;

    public TextAreaEditView(Context context) {
        this(context, null);
    }

    public TextAreaEditView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextAreaEditView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
        perseAttrs(attrs);
    }

    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_item_ta_et, this);
        mEditText = view.findViewById(R.id.et);
        mTvName = view.findViewById(R.id.tv_name);
        mTvLine = view.findViewById(R.id.tv_line);
        mEditText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //canScrollVertically()方法为判断指定方向上是否可以滚动,参数为正数或负数,负数检查向上是否可以滚动,正数为检查向下是否可以滚动
                if (mEditText.canScrollVertically(1) || mEditText.canScrollVertically(-1)) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);//requestDisallowInterceptTouchEvent();要求父类布局不在拦截触摸事件
                    if (event.getAction() == MotionEvent.ACTION_UP) { //判断是否松开
                        v.getParent().requestDisallowInterceptTouchEvent(false); //requestDisallowInterceptTouchEvent();让父类布局继续拦截触摸事件
                    }
                }
                return false;
            }
        });
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                teamTemp = charSequence;
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (onFocusChangedListener != null) {
                    onFocusChangedListener.onFocusChanged(TextAreaEditView.this, b);
                }
            }
        });
    }

    public void setTitleStr(String name){
        mTvName.setText(name);
    }
    public void setTvLine(int gone){
        mTvLine.setVisibility(gone);
    }
    /**
     * 设置输入框是否可编辑
     */
    public void setEnable(boolean isEnable) {
        mEditText.setFocusable(isEnable);
        mEditText.setFocusableInTouchMode(isEnable);
    }

    /**
     * @return
     */
    public void setEtHint(String s) {
        mEditText.setHint(s);
    }

    public void setMaxText(int max) {
        mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max) {
        }});
    }

    public EditText getEditText() {
        return mEditText;
    }

    public void setEditContent(int nameId) {
        mEditText.setText(nameId);
    }

    public void setEditContent(String name) {
        mEditText.setText(name);
    }

    public String getEditContent() {
        return mEditText.getText().toString();
    }

    public void setFoucs() {
        mEditText.setFocusable(false);
    }

    public void setFoucs(boolean isFoucs) {
        mEditText.setFocusable(isFoucs);
    }

    private void perseAttrs(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TaEditView);
            //遍历AttributeSet中的XML属性
            //设置hint
            mEditText.setHint(typedArray.getString(R.styleable.TaEditView_ta_edit_hint));
            //设置editText最多输入多少字
            mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(typedArray.getInteger(R.styleable.TaEditView_ta_edit_max_text, 500)) {
            }});
            MAX_LENGTH = typedArray.getInteger(R.styleable.TaEditView_ta_edit_max_text, 500);
            //设置输入类容
            mEditText.setText(typedArray.getString(R.styleable.TaEditView_ta_edit_content));
            // 是否显示底部输入数字
            int startPadding = (int) typedArray.getDimension(R.styleable.TaEditView_ta_edit_view_padding_start, ResolutionUtil.dp2px(context, 16));
            int topPadding = ResolutionUtil.dp2px(context, 10);
            int endPadding = (int) typedArray.getDimension(R.styleable.TaEditView_ta_edit_view_padding_end, ResolutionUtil.dp2px(context, 16));
            mEditText.setPadding(startPadding, topPadding, endPadding, 0);
            typedArray.recycle();
        }

    }

    @BindingAdapter(value = {"ta_edit_content"}, requireAll = false)
    public static void setText(TextAreaEditView view, String text) {
        if (text == null) {
            return;
        } else if (text.equals(((EditText) view.findViewById(R.id.et)).getText().toString())) {
            return;
        }
        ((EditText) view.findViewById(R.id.et)).setText(text);
    }

    public static void setHintText(TextAreaEditView view, String text) {
        if (text == null) {
            return;
        } else if (text.equals(((EditText) view.findViewById(R.id.et)).getHint().toString())) {
            return;
        }
        ((EditText) view.findViewById(R.id.et)).setHint(text);
    }

    @InverseBindingAdapter(attribute = "ta_edit_content", event = "textAttrChanged")
    public static String getText(TextAreaEditView view) {
        return ((EditText) view.findViewById(R.id.et)).getText().toString();
    }

    @BindingAdapter(value = {"textAttrChanged"}, requireAll = false)
    public static void setTextAttrChanged(TextAreaEditView view, final InverseBindingListener inverseBindingListener) {
        TextWatcher newWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                inverseBindingListener.onChange();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        TextWatcher oldWatcher = ListenerUtil.trackListener(((EditText) view.findViewById(R.id.et)), newWatcher, R.id.textWatcher);

        if (inverseBindingListener != null) {
            ((EditText) view.findViewById(R.id.et)).addTextChangedListener(newWatcher);
        }

        if (oldWatcher != null) {
            ((EditText) view.findViewById(R.id.et)).removeTextChangedListener(oldWatcher);
        }
    }

    /**
     * View焦点改变接口
     */
    public interface OnFocusChangedListener {
        void onFocusChanged(View view, boolean focused);
    }

    /**
     * 设置焦点改变的监听
     *
     * @param onFocusChangedListener
     */
    public void setViewOnFocusChangedListener(OnFocusChangedListener onFocusChangedListener) {
        this.onFocusChangedListener = onFocusChangedListener;
    }
}